package com.guanyong.admin.controllers.page;

import com.guanyong.utils.Cache.ICahce.ICache;
import com.guanyong.utils.NetUtils.CookieUtils;
import com.guanyong.utils.VerifyCode.VerifyCodeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by mxk on 2017/6/5.
 */
@Controller
@RequestMapping("mortise")
public class CommPage {
    @Value("${mortise.VerifyCode.cache.codekey}")
    String VerifyCode_cache_codeKey;
    @Value("${mortise.VerifyCode.cookie.codekey}")
    String VerifyCode_cookie_codeKey;
    @Autowired
    ICache cache;
    /**
     * 图形验证码
     * @param request
     * @param response
     */
    @RequestMapping("AuthImage")
    public void AuthImage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeHelper.generateVerifyCode(4);
        String CodeKey = VerifyCodeHelper.generateVerifyCode(8);
        cache.put(VerifyCode_cache_codeKey, CodeKey, verifyCode, 3600);//验证码放入缓存中，一小时有效期
        CookieUtils.addCookie(response,VerifyCode_cookie_codeKey,CodeKey,0,"","");

        //生成图片
        int w = 100, h = 30;
        VerifyCodeHelper.outputImage(w, h, response.getOutputStream(), verifyCode);


    }
}
